package cn.lg.soar.mvc.config;

import cn.lg.soar.common.util.IpUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j(topic = "access")
public class AccessLogFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        long start = System.nanoTime();
        filterChain.doFilter(servletRequest, servletResponse);
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        log.info(
                "{}\t{}\t{}\t{}",
                IpUtil.getRemoteIp(request),
                request.getServletPath(),
                response.getStatus(),
                (System.nanoTime() - start)
        );
    }

}